package com.ruoyi.fun.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fun.mapper.EcSalesReportMapper;
import com.ruoyi.fun.domain.EcSalesReport;
import com.ruoyi.fun.service.IEcSalesReportService;

/**
 * 商品销售报表Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-17
 */
@Service
public class EcSalesReportServiceImpl implements IEcSalesReportService 
{
    @Autowired
    private EcSalesReportMapper ecSalesReportMapper;

    /**
     * 查询商品销售报表列表
     * 
     * @param params 查询参数
     * @return 商品销售报表
     */
    @Override
    public List<EcSalesReport> selectSalesReportList(Map<String, Object> params)
    {
        List<EcSalesReport> reportList = ecSalesReportMapper.selectSalesReportList(params);
        
        // 计算衍生指标
        for (EcSalesReport report : reportList) {
            calculateDerivedMetrics(report);
        }
        
        return reportList;
    }

    /**
     * 根据商品ID查询销售报表详情
     * 
     * @param productId 商品ID
     * @return 商品销售报表
     */
    @Override
    public EcSalesReport selectSalesReportByProductId(Long productId)
    {
        EcSalesReport report = ecSalesReportMapper.selectSalesReportByProductId(productId);
        if (report != null) {
            calculateDerivedMetrics(report);
        }
        return report;
    }

    /**
     * 根据分类ID查询销售报表汇总
     * 
     * @param categoryId 分类ID
     * @return 商品销售报表
     */
    @Override
    public List<EcSalesReport> selectSalesReportByCategoryId(Long categoryId)
    {
        List<EcSalesReport> reportList = ecSalesReportMapper.selectSalesReportByCategoryId(categoryId);
        
        // 计算衍生指标
        for (EcSalesReport report : reportList) {
            calculateDerivedMetrics(report);
        }
        
        return reportList;
    }

    /**
     * 查询销售报表统计数据
     * 
     * @param params 查询参数
     * @return 统计数据
     */
    @Override
    public Map<String, Object> selectSalesReportStatistics(Map<String, Object> params)
    {
        return ecSalesReportMapper.selectSalesReportStatistics(params);
    }

    /**
     * 导出商品销售报表数据
     * 
     * @param params 查询参数
     * @return 商品销售报表
     */
    @Override
    public List<EcSalesReport> exportSalesReportList(Map<String, Object> params)
    {
        List<EcSalesReport> reportList = ecSalesReportMapper.selectSalesReportList(params);
        
        // 计算衍生指标
        for (EcSalesReport report : reportList) {
            calculateDerivedMetrics(report);
        }
        
        return reportList;
    }

    /**
     * 计算衍生指标
     * 
     * @param report 销售报表对象
     */
    private void calculateDerivedMetrics(EcSalesReport report) {
        if (report == null) {
            return;
        }

        // 计算退货率
        if (report.getSaleCount() != null && report.getSaleCount() > 0 
            && report.getRefundCount() != null) {
            BigDecimal refundRate = new BigDecimal(report.getRefundCount())
                .divide(new BigDecimal(report.getSaleCount()), 4, RoundingMode.HALF_UP)
                .multiply(new BigDecimal(100));
            report.setRefundRate(refundRate);
        }

        // 计算毛利率
        if (report.getSaleAmount() != null && report.getSaleAmount().compareTo(BigDecimal.ZERO) > 0
            && report.getCostPrice() != null) {
            BigDecimal grossProfitRate = report.getSaleAmount().subtract(report.getCostPrice())
                .divide(report.getSaleAmount(), 4, RoundingMode.HALF_UP)
                .multiply(new BigDecimal(100));
            report.setGrossProfitRate(grossProfitRate);
        }

        // 计算库存周转率
        if (report.getCostPrice() != null && report.getCostPrice().compareTo(BigDecimal.ZERO) > 0
            && report.getAvgStock() != null && report.getAvgStock().compareTo(BigDecimal.ZERO) > 0) {
            BigDecimal inventoryTurnoverRate = report.getCostPrice()
                .divide(report.getAvgStock(), 4, RoundingMode.HALF_UP);
            report.setStockTurnoverRate(inventoryTurnoverRate);
        }
    }
}